/*******************************************************************************
 * Copyright 2007 Vidar Svansson
 *
 * This file is part of JAdapter.
 *
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/


package org.jadapter.tests;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import org.jadapter.registry.AdapterRegistry;
import org.jadapter.registry.TransitiveAdapterRegistry;
import org.jadapter.tests.examples.CommaSeparated;
import org.jadapter.tests.examples.ConstructorAdapter;


public class BasicUsage extends TestCase {
    
    
    @SuppressWarnings("serial")
	public void testAdapterBasicUsage() throws Exception {
        // Before we test using JAdapter, we test the components
        
        // this is the context object that we want to adapt to some type    
        Map<String, String> map = new HashMap<String,String>(){{
            put("Foo", "foo");
            put("Bar", "bar");
        }};
        
        // this is our target, CommaSeparated is an interface
        CommaSeparated commas;
        
        // This is a simple adapter that implements CommaSeparated and
        // has a constructor accepting a single Map instance
        commas = new ConstructorAdapter(map);
        assertEquals("Foo:foo,Bar:bar", commas.getCommaSeparated());
        
        
        // OK, lets do that with JAdapter
        AdapterRegistry registry = new TransitiveAdapterRegistry();
        
        // we are using the API directly here to register the adapter 
        // before we use it, normally this would be somewhere in the
        // initialization of the application, e.g. by using an IoC
        // framework.
        registry.register(
                ConstructorAdapter.class, // the provided type
                CommaSeparated.class, // the adapter implementing above
                Map.class); // the type of the context objects
        
        // if you don't prefer to call a function with multiple parameters of
        // the same type, then you can pass a single Adapter instance to register
            
        // now JAdapter knows that we can use CommaSeparatedMap when we need to
        // adapt Map -> CommaSeparated
        // so we simply ask the component registry to give us a CommaSeparated 
        // instance for the map object
        commas = registry.transform(map, CommaSeparated.class);
        assertEquals("Foo:foo,Bar:bar", commas.getCommaSeparated());
        
    }    

       
}
